package com.vcc.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vcc.core.exception.NotFoundException;
import com.vcc.core.exception.ProgramException;
import com.vcc.data.entity.BossRole;
import com.vcc.data.mapper.BossRoleMapper;
import com.vcc.service.service.BossRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.vcc.data.entity.table.BossRoleTableDef.BOSS_ROLE;

@Service
public class BossRoleServiceImpl extends ServiceImpl<BossRoleMapper, BossRole> implements BossRoleService {


    @Override
    public Page<BossRole> paging(Page<BossRole> pageable, String roleCode, String roleName, Boolean hasAllData, Boolean hasParamStatus) {
        return queryChain()
                .where(BOSS_ROLE.ROLE_NAME.like(roleName, StringUtils.isNotBlank(roleName)))
                .where(BOSS_ROLE.ROLE_CODE.like(roleCode, StringUtils.isNotBlank(roleCode)))
                .where(BOSS_ROLE.HAS_ALL_DATA.eq(hasAllData, hasAllData != null))
                .where(BOSS_ROLE.HAS_PARAM_STATUS.eq(hasParamStatus, hasParamStatus != null))
                .orderBy(BOSS_ROLE.CREATE_DATE.desc())
                .page(pageable);
    }

    @Override
    public List<BossRole> listAll(String roleName) {
        return queryChain()
                .where(BOSS_ROLE.ROLE_NAME.like(roleName, StringUtils.isNotBlank(roleName)))
                .orderBy(BOSS_ROLE.CREATE_DATE.desc())
                .list();
    }

    @Override
    public BossRole getByUserId(Long userId) {
        return mapper.findByUserId(userId).orElseThrow(() -> new NotFoundException("当前用户没有绑定角色"));
    }

    @Override
    public List<BossRole> listByUserId(Long userId) {
        return mapper.findAllByUserId(userId);
    }

    @Override
    public Boolean existsById(Long id) {
        return queryChain().eq(BossRole::getId, id).exists();
    }

    @Override
    public BossRole createRole(String roleCode, String roleName, Boolean hasAllData) {
        if (queryChain().eq(BossRole::getRoleCode, roleCode).exists()) {
            throw new ProgramException("角色代码已存在");
        }
        if (queryChain().eq(BossRole::getRoleName, roleName).exists()) {
            throw new ProgramException("角色名称已存在");
        }
        BossRole bossRole = new BossRole();
        bossRole.setRoleCode(roleCode);
        bossRole.setRoleName(roleName);
        bossRole.setHasAllData(hasAllData);
        bossRole.setStatus(0);
        bossRole.setHasParamStatus(true);
        save(bossRole);
        return bossRole;
    }

    @Override
    public Boolean updateRole(Long id, Boolean hasAllData) {
        return updateChain()
                .set(BossRole::getHasAllData, hasAllData)
                .eq(BossRole::getId, id)
                .update();
    }

    @Override
    public Boolean updateRoleHasParamStatus(Long id, Boolean hasParamStatus) {
        return updateChain()
                .set(BossRole::getHasParamStatus, hasParamStatus)
                .eq(BossRole::getId, id)
                .update();
    }
}
